<p>LazyLoad is a lightweight (2.4 kB) and flexible script that <strong>speeds up your web application</strong> by deferring the loading of your below-the-fold images, videos and iframes to <strong>when they will enter the viewport</strong>. It’s written in plain “vanilla” JavaScript, it leverages the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API">IntersectionObserver</a> API, it supports <a href="https://alistapart.com/article/responsive-images-in-practice">responsive images</a>, it optimizes your website for slower connections, and can enable native lazy loading. See <a href="#-notable-features">notable features</a> for more.</p>
<p><a href="https://www.npmjs.com/package/vanilla-lazyload"><img src="https://img.shields.io/npm/v/vanilla-lazyload/latest.svg" alt="vanilla-lazyload (latest)"></a><br><a href="https://www.npmjs.com/package/vanilla-lazyload"><img src="https://img.shields.io/npm/dy/vanilla-lazyload.svg" alt="vanilla-lazyload (downloads)"></a><br><a href="https://www.jsdelivr.com/package/npm/vanilla-lazyload"><img src="https://data.jsdelivr.com/v1/package/npm/vanilla-lazyload/badge"></a></p>
<p>➡️ Jump to: <a href="#-getting-started---html">👨‍💻 Getting started - HTML</a> - <a href="#-getting-started---script">👩‍💻 Getting started - Script</a> - <a href="#-recipes">🥧 Recipes</a> - <a href="#-demos">📺 Demos</a> - <a href="#-tips--tricks">😋 Tips &amp; tricks</a> - <a href="#-api">🔌 API</a> - <a href="#-notable-features">😯 Notable features</a></p>
<hr>
<p><strong>Love this project? 😍 <a href="https://ko-fi.com/verlok">Buy me a coffee!</a></strong></p>
<hr>
<h2 id="👨‍💻-Getting-started-HTML"><a href="#👨‍💻-Getting-started-HTML" class="headerlink" title="👨‍💻 Getting started - HTML"></a>👨‍💻 Getting started - HTML</h2><p>In order to make your content be loaded by LazyLoad, you must use some <code>data-</code> attributes instead of the actual attributes. Examples below.</p>
<h4 id="Lazy-image"><a href="#Lazy-image" class="headerlink" title="Lazy image:"></a>Lazy image:</h4><pre><code class="html">&lt;img alt=&quot;A lazy image&quot; data-src=&quot;lazy.jpg&quot; /&gt;
</code></pre>
<h4 id="Lazy-image-with-low-quality-placeholder"><a href="#Lazy-image-with-low-quality-placeholder" class="headerlink" title="Lazy image with low quality placeholder:"></a>Lazy image with low quality placeholder:</h4><pre><code class="html">&lt;img
  alt=&quot;A lazy image&quot;
  src=&quot;lazy-lowQuality.jpg&quot;
  data-src=&quot;lazy.jpg&quot;
/&gt;
</code></pre>
<h4 id="Lazy-responsive-image-with-srcset-and-sizes"><a href="#Lazy-responsive-image-with-srcset-and-sizes" class="headerlink" title="Lazy responsive image with srcset and sizes:"></a>Lazy responsive image with <code>srcset</code> and <code>sizes</code>:</h4><pre><code class="html">&lt;img
  alt=&quot;A lazy image&quot;
  class=&quot;lazy&quot;
  data-src=&quot;lazy.jpg&quot;
  data-srcset=&quot;lazy_400.jpg 400w, 
    lazy_800.jpg 800w&quot;
  data-sizes=&quot;100w&quot;
/&gt;
</code></pre>
<p>To have a low quality placeholder, add the <code>src</code> attribute pointing to a very small version of the image. E.g. <code>src=&quot;lazy_10.jpg&quot;</code>.</p>
<h4 id="Lazy-responsive-image-with-hi-dpi-support-using-the-picture-tag"><a href="#Lazy-responsive-image-with-hi-dpi-support-using-the-picture-tag" class="headerlink" title="Lazy responsive image with hi-dpi support using the picture tag:"></a>Lazy responsive image with hi-dpi support using the <code>picture</code> tag:</h4><pre><code class="html">&lt;picture&gt;
  &lt;source
    media=&quot;(min-width: 1200px)&quot;
    data-srcset=&quot;lazy_1200.jpg 1x, 
      lazy_2400.jpg 2x&quot;
  /&gt;
  &lt;source
    media=&quot;(min-width: 800px)&quot;
    data-srcset=&quot;lazy_800.jpg 1x, 
      lazy_1600.jpg 2x&quot;
  /&gt;
  &lt;img
    alt=&quot;A lazy image&quot;
    class=&quot;lazy&quot;
    data-src=&quot;lazy.jpg&quot;
  /&gt;
&lt;/picture&gt;
</code></pre>
<p>To have a low quality placeholder, add the <code>src</code> attribute pointing to a very small version of the image to the <code>img</code> tag. E.g. <code>src=&quot;lazy_10.jpg&quot;</code>.</p>
<h4 id="Lazy-responsive-image-with-automatic-WebP-format-selection-using-the-picture-tag"><a href="#Lazy-responsive-image-with-automatic-WebP-format-selection-using-the-picture-tag" class="headerlink" title="Lazy responsive image with automatic WebP format selection, using the picture tag:"></a>Lazy responsive image with automatic <em>WebP</em> format selection, using the <code>picture</code> tag:</h4><pre><code class="html">&lt;picture&gt;
  &lt;source
    type=&quot;image/webp&quot;
    data-srcset=&quot;lazy_400.webp 400w, 
      lazy_800.webp 800w&quot;
    data-sizes=&quot;100w&quot;
  /&gt;
  &lt;img
    alt=&quot;A lazy image&quot;
    class=&quot;lazy&quot;
    data-src=&quot;lazy.jpg&quot;
    data-srcset=&quot;lazy_400.jpg 400w, 
      lazy_800.jpg 800w&quot;
    data-sizes=&quot;100w&quot;
  /&gt;
&lt;/picture&gt;
</code></pre>
<p>To have a low quality placeholder, add the <code>src</code> attribute pointing to a very small version of the image to the <code>img</code> tag. E.g. <code>src=&quot;lazy_10.jpg&quot;</code>.</p>
<h4 id="Lazy-background-image"><a href="#Lazy-background-image" class="headerlink" title="Lazy background image"></a>Lazy background image</h4><p>⚠ <strong>IMPORTANT NOTE</strong>: To display content images on your pages, always use the <code>img</code> tag. This would benefit the SEO and the accessibility of your website. To understand if your images are content or background, ask yourself: “would my website user like to see those images when printing out the page?”. If the answer is “yes”, then your images are content images and you should avoid using background images to display them.</p>
<p>Single background image:</p>
<pre><code class="html">&lt;div class=&quot;lazy&quot; data-bg=&quot;lazy.jpg&quot;&gt;&lt;/div&gt;
</code></pre>
<p>Single background, with HiDPI screen support:</p>
<pre><code class="html">&lt;div
  class=&quot;lazy&quot;
  data-bg=&quot;lazy.jpg&quot;
  data-bg-hidpi=&quot;lazy@2x.jpg&quot;
&gt;&lt;/div&gt;
</code></pre>
<p>Multiple backgrounds:</p>
<pre><code class="html">&lt;div
  class=&quot;lazy&quot;
  data-bg-multi=&quot;url(lazy-head.jpg), 
    url(lazy-body.jpg), 
    linear-gradient(#fff, #ccc)&quot;
&gt;
  ...
&lt;/div&gt;
</code></pre>
<p>ℹ Please note that you must use <code>url()</code> to wrap the URLs in your <code>data-bg-multi</code> attributes.</p>
<p>Multiple backgrounds, HiDPI screen support:</p>
<pre><code class="html">&lt;div
  class=&quot;lazy&quot;
  data-bg-multi=&quot;url(lazy-head.jpg),
    url(lazy-body.jpg),
    linear-gradient(#fff, #ccc)&quot;
  data-bg-multi-hidpi=&quot;url(lazy-head@2x.jpg),
    url(lazy-body@2x.jpg),
    linear-gradient(#fff, #ccc)&quot;
&gt;
  ...
&lt;/div&gt;
</code></pre>
<p>ℹ Please note that you must use <code>url()</code> to wrap the URLs in your <code>data-bg-multi-hidpi</code> attributes.</p>
<h4 id="Lazy-video"><a href="#Lazy-video" class="headerlink" title="Lazy video"></a>Lazy video</h4><pre><code class="html">&lt;video
  class=&quot;lazy&quot;
  controls
  width=&quot;620&quot;
  data-src=&quot;lazy.mp4&quot;
  data-poster=&quot;lazy.jpg&quot;
&gt;
  &lt;source type=&quot;video/mp4&quot; data-src=&quot;lazy.mp4&quot; /&gt;
  &lt;source type=&quot;video/ogg&quot; data-src=&quot;lazy.ogg&quot; /&gt;
  &lt;source type=&quot;video/avi&quot; data-src=&quot;lazy.avi&quot; /&gt;
&lt;/video&gt;
</code></pre>
<p>Please note that the video poster can be lazily loaded too.</p>
<h4 id="Lazy-iframe"><a href="#Lazy-iframe" class="headerlink" title="Lazy iframe"></a>Lazy iframe</h4><pre><code class="html">&lt;iframe class=&quot;lazy&quot; data-src=&quot;lazyFrame.html&quot;&gt;&lt;/iframe&gt;
</code></pre>
<hr>
<p><strong>Love this project? 😍 <a href="https://ko-fi.com/verlok">Buy me a coffee!</a></strong></p>
<hr>
<h2 id="👩‍💻-Getting-started-Script"><a href="#👩‍💻-Getting-started-Script" class="headerlink" title="👩‍💻 Getting started - Script"></a>👩‍💻 Getting started - Script</h2><p>The latest, recommended version of LazyLoad is <strong>17.1.0</strong>.</p>
<p>Quickly understand how to upgrade from a previous version reading the <a href="UPGRADE.md">practical upgrade guide</a>.</p>
<h3 id="To-polyfill-or-not-to-polyfill-IntersectionObserver"><a href="#To-polyfill-or-not-to-polyfill-IntersectionObserver" class="headerlink" title="To polyfill or not to polyfill IntersectionObserver?"></a>To polyfill or not to polyfill IntersectionObserver?</h3><p>On browser NOT supporting IntersectionObserver such as Internet explorer and older versions of Safari <strong>you can choose whether or not to add a javascript polyfill</strong> for it.</p>
<p>If you <strong>don’t use a polyfill</strong>, LazyLoad will <strong>load all the images</strong> as soon as it’s downloaded and executed. The number of impacted users would be <a href="https://caniuse.com/intersectionobserver">relatively small</a>, so this is a completely acceptable choice.</p>
<p>If you prefer to load a polyfill, the regular LazyLoad behaviour is granted.</p>
<h3 id="The-simple-easiest-way"><a href="#The-simple-easiest-way" class="headerlink" title="The simple, easiest way"></a>The simple, easiest way</h3><p>The easiest way to use LazyLoad is to include the script from a CDN:</p>
<pre><code class="html">&lt;script src=&quot;https://cdn.jsdelivr.net/npm/vanilla-lazyload@17.1.0/dist/lazyload.min.js&quot;&gt;&lt;/script&gt;
</code></pre>
<p>Or, with the IntersectionObserver polyfill:</p>
<pre><code class="html">&lt;script src=&quot;https://cdn.jsdelivr.net/npm/intersection-observer@0.7.0/intersection-observer.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/vanilla-lazyload@17.1.0/dist/lazyload.min.js&quot;&gt;&lt;/script&gt;
</code></pre>
<p>Then, in your javascript code:</p>
<pre><code class="js">var lazyLoadInstance = new LazyLoad({
  // Your custom settings go here
});
</code></pre>
<p>To be sure that DOM for your lazy content is ready when you instantiate LazyLoad, <strong>place the script tag right before the closing <code>&lt;/body&gt;</code> tag</strong>. If more DOM arrives later, e.g. via an AJAX call, you’ll need to call <code>lazyLoadInstance.update();</code> to make LazyLoad check the DOM again.</p>
<pre><code class="js">lazyLoadInstance.update();
</code></pre>
<h3 id="Include-via-RequireJS"><a href="#Include-via-RequireJS" class="headerlink" title="Include via RequireJS"></a>Include via RequireJS</h3><p>You can use <a href="https://requirejs.org/">RequireJS</a> to dynamically and asynchronously load modules in your website.</p>
<p>You can also find the original W3C’S <a href="https://github.com/verlok/IntersectionObserverAMD">IntersectionObserver Polyfill packed in AMD</a> so you can <code>require</code> it conditionally, along with LazyLoad.</p>
<p>Include RequireJS:</p>
<pre><code class="html">&lt;script src=&quot;https://cdn.jsdelivr.net/npm/requirejs@2.3.6/bin/r.min.js&quot;&gt;&lt;/script&gt;
</code></pre>
<p>Then <code>require</code> the AMD version of LazyLoad, like this:</p>
<pre><code class="js">var lazyLoadAmdUrl = &quot;https://cdn.jsdelivr.net/npm/vanilla-lazyload@17.1.0/dist/lazyload.amd.min.js&quot;;
var polyfillAmdUrl = &quot;https://cdn.jsdelivr.net/npm/intersection-observer-amd@2.0.1/intersection-observer-amd.js&quot;;

/// Dynamically define the dependencies
var dependencies = [
    &quot;IntersectionObserver&quot; in window
        ? null // &lt;- Doesn&#39;t require the polyfill
        : polyfillAmdUrl,
    lazyLoadAmdUrl
];

// Initialize LazyLoad inside the callback
require(dependencies, function(_, LazyLoad) {
    var lazyLoadInstance = new LazyLoad({
        // Your custom settings go here
    });
}
</code></pre>
<p><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/amd_polyfill.html">DEMO</a> - <a href="https://github.com/verlok/vanilla-lazyload/blob/master/demos/amd_polyfill.html">SOURCE</a></p>
<h3 id="Using-an-async-script"><a href="#Using-an-async-script" class="headerlink" title="Using an async script"></a>Using an <code>async</code> script</h3><p>If you prefer, it’s possible to include LazyLoad’s script using <code>async</code> script and initialize it as soon as it’s loaded.</p>
<p>To do so, <strong>you must define the options before including the script</strong>. You can pass:</p>
<ul>
<li><code>{}</code> an object to get a single instance of LazyLoad</li>
<li><code>[{}, {}]</code> an array of objects to get multiple instances of LazyLoad, each one with different options.</li>
</ul>
<pre><code class="html">&lt;script&gt;
  // Set the options globally
  // to make LazyLoad self-initialize
  window.lazyLoadOptions = {
    // Your custom settings go here
  };
&lt;/script&gt;
</code></pre>
<p>Then include the script.</p>
<pre><code class="html">&lt;script
  async
  src=&quot;https://cdn.jsdelivr.net/npm/vanilla-lazyload@17.1.0/dist/lazyload.min.js&quot;
&gt;&lt;/script&gt;
</code></pre>
<p><strong>Possibly place the script tag right before the closing <code>&lt;/body&gt;</code> tag</strong>. If you can’t do that, LazyLoad could be executed before the browser has loaded all the DOM, and you’ll need to call its <code>update()</code> method to make it check the DOM again.</p>
<h3 id="Using-an-async-script-getting-the-instance-reference"><a href="#Using-an-async-script-getting-the-instance-reference" class="headerlink" title="Using an async script + getting the instance reference"></a>Using an <code>async</code> script + getting the instance reference</h3><p>Same as above, but you must put the <code>addEventListener</code> code shown below before including the <code>async</code> script.</p>
<pre><code class="html">&lt;script&gt;
  // Set the options globally
  // to make LazyLoad self-initialize
  window.lazyLoadOptions = {
    // Your custom settings go here
  };
  // Listen to the initialization event
  // and get the instance of LazyLoad
  window.addEventListener(
    &quot;LazyLoad::Initialized&quot;,
    function (event) {
      window.lazyLoadInstance = event.detail.instance;
    },
    false
  );
&lt;/script&gt;
</code></pre>
<p>Then include the script.</p>
<pre><code class="html">&lt;script
  async
  src=&quot;https://cdn.jsdelivr.net/npm/vanilla-lazyload@17.1.0/dist/lazyload.min.js&quot;
&gt;&lt;/script&gt;
</code></pre>
<p>Now you’ll be able to call its methods, like:</p>
<pre><code class="js">lazyLoadInstance.update();
</code></pre>
<p>Note about Internet Explorer: because this technique uses a <code>CustomEvent</code> to trigger the <code>LazyLoad::Initialized</code> event, you might want to add <a href="https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent#Polyfill">this polyfill</a> to make it work on Internet Explorer.</p>
<p><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/async.html">DEMO</a> - <a href="https://github.com/verlok/vanilla-lazyload/blob/master/demos/async.html">SOURCE</a> &amp;larr; for a single LazyLoad instance</p>
<p><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/async_multiple.html">DEMO</a> - <a href="https://github.com/verlok/vanilla-lazyload/blob/master/demos/async_multiple.html">SOURCE</a> &amp;larr; for multiple LazyLoad instances</p>
<h3 id="Local-install"><a href="#Local-install" class="headerlink" title="Local install"></a>Local install</h3><p>If you prefer to install LazyLoad locally in your project, you can!</p>
<h4 id="Using-npm"><a href="#Using-npm" class="headerlink" title="Using npm"></a>Using npm</h4><pre><code>npm install vanilla-lazyload
</code></pre>
<h4 id="Using-bower"><a href="#Using-bower" class="headerlink" title="Using bower"></a>Using bower</h4><pre><code>bower install vanilla-lazyload
</code></pre>
<h4 id="Manual-download"><a href="#Manual-download" class="headerlink" title="Manual download"></a>Manual download</h4><p>Download one the latest <a href="https://github.com/verlok/vanilla-lazyload/releases/">releases</a>. The files you need are inside the <code>dist</code> folder. If you don’t know which one to pick, use <code>lazyload.min.js</code>, or read <a href="#bundles">about bundles</a>.</p>
<h3 id="Local-usage"><a href="#Local-usage" class="headerlink" title="Local usage"></a>Local usage</h3><p>Should you install LazyLoad locally, you can import it as ES module like the following:</p>
<pre><code class="js">import LazyLoad from &quot;vanilla-lazyload&quot;;
</code></pre>
<p>It’s also possible (but unadvised) to use the <code>require</code> commonJS syntax.</p>
<p>More information about bundling LazyLoad with WebPack are available on <a href="https://github.com/verlok/vanilla-lazyload-es2015-webpack-test">this specific repo</a>.</p>
<h3 id="Usage-with-React"><a href="#Usage-with-React" class="headerlink" title="Usage with React"></a>Usage with React</h3><p>Take a look at this example of <a href="https://codesandbox.io/s/20306yk96p">usage of React with LazyLoad</a> on Sandbox.</p>
<p>This implementation takes the same props that you would normally pass to the <code>img</code> tag, but it renders a lazy image. Feel free to fork and improve it!</p>
<h3 id="Bundles"><a href="#Bundles" class="headerlink" title="Bundles"></a>Bundles</h3><p>Inside the <code>dist</code> folder you will find different bundles.</p>
<table>
<thead>
<tr>
<th>Filename</th>
<th>Module Type</th>
<th>Advantages</th>
</tr>
</thead>
<tbody><tr>
<td><code>lazyload.min.js</code></td>
<td>UMD <small>(Universal Module Definition)</small></td>
<td>Works pretty much everywhere, even in common-js contexts</td>
</tr>
<tr>
<td><code>lazyload.iife.min.js</code></td>
<td>IIFE <small>(Immediately Invoked Function Expression)</small></td>
<td>Works as in-page <code>&lt;script src=&quot;...&quot;&gt;</code>, ~0.5kb smaller than UMD version</td>
</tr>
<tr>
<td><code>lazyload.amd.min.js</code></td>
<td>AMD <small>(Asynchronous Module Definition)</small></td>
<td>Works with <em>RequireJS</em> module loader, ~0.5kb smaller than UMD version</td>
</tr>
<tr>
<td><code>lazyload.esm.js</code></td>
<td>ES Module</td>
<td>Exports <code>LazyLoad</code> so you can import it in your project both using <code>&lt;script type=&quot;module&quot; src=&quot;...&quot;&gt;</code> and a bundler like WebPack or Rollup</td>
</tr>
</tbody></table>
<hr>
<p><strong>Love this project? 😍 <a href="https://ko-fi.com/verlok">Buy me a coffee!</a></strong></p>
<hr>
<h2 id="🥧-Recipes"><a href="#🥧-Recipes" class="headerlink" title="🥧 Recipes"></a>🥧 Recipes</h2><p>This is the section where you can find <em>ready to copy &amp; paste</em> code for your convenience.</p>
<h3 id="Dynamic-content"><a href="#Dynamic-content" class="headerlink" title="Dynamic content"></a>Dynamic content</h3><blockquote>
<p>💡 <strong>Use case</strong>: when you want to lazily load images, but the number of images change in the scrolling area changes, maybe because they are added asynchronously.</p>
</blockquote>
<p>HTML</p>
<p><em>The HTML to use depends on your case, see other recipes’ HTML</em></p>
<p>Javascript</p>
<pre><code class="js">var myLazyLoad = new LazyLoad();
// After your content has changed...
myLazyLoad.update();
</code></pre>
<p><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/dynamic_content.html">DEMO</a> - <a href="https://github.com/verlok/vanilla-lazyload/blob/master/demos/dynamic_content.html">SOURCE</a> - <a href="#-api">API</a></p>
<h3 id="Mixed-native-and-JS-based-lazy-loading"><a href="#Mixed-native-and-JS-based-lazy-loading" class="headerlink" title="Mixed native and JS-based lazy loading"></a>Mixed native and JS-based lazy loading</h3><blockquote>
<p>💡 <strong>Use case</strong>: you want to use the <code>use_native</code> option to delegate the loading of images to the browsers engine where supported, but you also want to lazily load backgroud images or videos.</p>
</blockquote>
<p>HTML</p>
<pre><code class="html">&lt;img class=&quot;lazy&quot; alt=&quot;A lazy image&quot; data-src=&quot;lazy.jpg&quot; /&gt;
&lt;iframe class=&quot;lazy&quot; data-src=&quot;lazyFrame.html&quot;&gt;&lt;/iframe&gt;
&lt;video
  class=&quot;lazy&quot;
  controls
  data-src=&quot;lazy.mp4&quot;
  data-poster=&quot;lazy.jpg&quot;
&gt;
  ...
&lt;/video&gt;
&lt;div class=&quot;lazy&quot; data-bg=&quot;lazy.jpg&quot;&gt;&lt;/div&gt;
</code></pre>
<p>Javascript</p>
<pre><code class="js">// Instance using native lazy loading
const lazyContent = new LazyLoad({
  use_native: true // &lt;-- there you go
});

// Instance without native lazy loading
const lazyBackground = new LazyLoad({
  // DON&#39;T PASS use_native: true HERE
});
</code></pre>
<h3 id="Scrolling-panel-s"><a href="#Scrolling-panel-s" class="headerlink" title="Scrolling panel(s)"></a>Scrolling panel(s)</h3><blockquote>
<p>💡 <strong>Use case</strong>: when your scrolling container is not the main browser window, but a scrolling container.</p>
</blockquote>
<p>HTML</p>
<pre><code class="html">&lt;div class=&quot;scrollingPanel&quot; id=&quot;scrollingPanel&quot;&gt;
  &lt;!-- Set of images --&gt;
&lt;/div&gt;
</code></pre>
<p>Javascript</p>
<pre><code class="js">var myLazyLoad = new LazyLoad({
  container: document.getElementById(&quot;scrollingPanel&quot;)
});
</code></pre>
<p><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/container_single.html">DEMO</a> - <a href="https://github.com/verlok/vanilla-lazyload/blob/master/demos/container_single.html">SOURCE</a> - <a href="#-api">API</a></p>
<p>If you have <em>multiple</em> scrolling panels, you can use the following markup and code.</p>
<p>HTML</p>
<pre><code class="html">&lt;div id=&quot;scrollingPanel1&quot; class=&quot;scrollingPanel&quot;&gt;
  &lt;!-- Set of images --&gt;
&lt;/div&gt;
&lt;div id=&quot;scrollingPanel2&quot; class=&quot;scrollingPanel&quot;&gt;
  &lt;!-- Set of images --&gt;
&lt;/div&gt;
</code></pre>
<p>Javascript</p>
<pre><code class="js">var myLazyLoad1 = new LazyLoad({
  container: document.getElementById(&quot;scrollingPanel1&quot;)
});
var myLazyLoad2 = new LazyLoad({
  container: document.getElementById(&quot;scrollingPanel2&quot;)
});
</code></pre>
<p><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/container_multiple.html">DEMO</a> - <a href="https://github.com/verlok/vanilla-lazyload/blob/master/demos/container_multiple.html">SOURCE</a> - <a href="#-api">API</a></p>
<h3 id="Lazy-functions"><a href="#Lazy-functions" class="headerlink" title="Lazy functions"></a>Lazy functions</h3><blockquote>
<p>💡 <strong>Use case</strong>: when you want to execute arbitrary scripts or functions when given elements enter the viewport</p>
</blockquote>
<p>HTML</p>
<pre><code class="html">&lt;div class=&quot;lazy&quot; data-lazy-function=&quot;foo&quot;&gt;...&lt;/div&gt;
&lt;div class=&quot;lazy&quot; data-lazy-function=&quot;bar&quot;&gt;...&lt;/div&gt;
&lt;div class=&quot;lazy&quot; data-lazy-function=&quot;buzz&quot;&gt;...&lt;/div&gt;
&lt;div class=&quot;lazy&quot; data-lazy-function=&quot;booya&quot;&gt;...&lt;/div&gt;
</code></pre>
<p>JS</p>
<pre><code class="js">// It&#39;s a best practice to scope the function names inside a namespace like `lazyFunctions`.
window.lazyFunctions = {
  foo: function (element) {
    element.style.color = &quot;red&quot;;
    console.log(&quot;foo&quot;);
  },
  bar: function (element) {
    element.remove(element);
    console.log(&quot;bar&quot;);
  },
  buzz: function (element) {
    var span = document.createElement(&quot;span&quot;);
    span.innerText = &quot; - buzz!&quot;;
    element.appendChild(span);
    console.log(&quot;buzz&quot;);
  },
  booya: function (element) {
    element.classList.add(&quot;boo&quot;);
    console.log(&quot;booya&quot;);
  }
};
</code></pre>
<pre><code class="js">function executeLazyFunction(element) {
  var lazyFunctionName = element.getAttribute(
    &quot;data-lazy-function&quot;
  );
  var lazyFunction = window.lazyFunctions[lazyFunctionName];
  if (!lazyFunction) return;
  lazyFunction(element);
}

var ll = new LazyLoad({
  unobserve_entered: true, // &lt;- Avoid executing the function multiple times
  callback_enter: executeLazyFunction // Assigning the function defined above
});
</code></pre>
<p>Use <code>unobserve_entered</code> to avoid executing the function multiple times.</p>
<p>That’s it. Whenever an element with the <code>data-lazy-function</code> attribute enters the viewport, LazyLoad calls the <code>executeLazyScript</code> function, which gets the function name from the <code>data-lazy-function</code> attribute itself and executes it.</p>
<p><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/lazy_functions.html">DEMO</a> - <a href="https://github.com/verlok/vanilla-lazyload/blob/master/demos/lazy_functions.html">SOURCE</a> - <a href="#-api">API</a></p>
<h3 id="Lazy-initialization-of-multiple-LazyLoad-instances"><a href="#Lazy-initialization-of-multiple-LazyLoad-instances" class="headerlink" title="Lazy initialization of multiple LazyLoad instances"></a>Lazy initialization of multiple LazyLoad instances</h3><blockquote>
<p>💡 <strong>Use case</strong>: when you have a lot of horizontally scrolling containers and you want to instantiate a LazyLoad instance on them, but only when they entered the viewport.</p>
</blockquote>
<p>HTML</p>
<pre><code class="html">&lt;div class=&quot;horizContainer&quot;&gt;
  &lt;img
    src=&quot;&quot;
    alt=&quot;Row 01, col 01&quot;
    data-src=&quot;https://placeholdit.imgix.net/~text?txtsize=19&amp;amp;txt=row_01_col_01&amp;amp;w=200&amp;amp;h=200&quot;
  /&gt;
  &lt;img
    src=&quot;&quot;
    alt=&quot;Row 01, col 02&quot;
    data-src=&quot;https://placeholdit.imgix.net/~text?txtsize=19&amp;amp;txt=row_01_col_02&amp;amp;w=200&amp;amp;h=200&quot;
  /&gt;
  &lt;!-- ... --&gt;
&lt;/div&gt;
&lt;div class=&quot;horizContainer&quot;&gt;
  &lt;img
    src=&quot;&quot;
    alt=&quot;Row 02, col 01&quot;
    data-src=&quot;https://placeholdit.imgix.net/~text?txtsize=19&amp;amp;txt=row_02_col_01&amp;amp;w=200&amp;amp;h=200&quot;
  /&gt;
  &lt;img
    src=&quot;&quot;
    alt=&quot;Row 02, col 02&quot;
    data-src=&quot;https://placeholdit.imgix.net/~text?txtsize=19&amp;amp;txt=row_02_col_02&amp;amp;w=200&amp;amp;h=200&quot;
  /&gt;
  &lt;!-- ... --&gt;
&lt;/div&gt;
</code></pre>
<p>Javascript</p>
<pre><code class="js">var lazyLoadInstances = [];

var initOneLazyLoad = function (horizContainerElement) {
  // When the .horizContainer element enters the viewport,
  // instantiate a new LazyLoad on the horizContainerElement
  var oneLL = new LazyLoad({
    container: horizContainerElement
  });
  // Optionally push it in the lazyLoadInstances
  // array to keep track of the instances
  lazyLoadInstances.push(oneLL);
};

// The &quot;lazyLazy&quot; instance of lazyload is used to check
// when the .horizContainer divs enter the viewport
var lazyLazy = new LazyLoad({
  elements_selector: &quot;.horizContainer&quot;,
  callback_enter: initOneLazyLoad,
  unobserve_entered: true // Stop observing .horizContainer(s) after they entered
});
</code></pre>
<p>That’s it. Whenever a <code>.horizContainer</code> element enters the viewport, LazyLoad calls the <code>initOneLazyLoad</code> function, which creates a new instance of LazyLoad on the <code>.horizContainer</code> element.</p>
<p><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/lazily_load_lazyLoad.html">DEMO</a> - <a href="https://github.com/verlok/vanilla-lazyload/blob/master/demos/lazily_load_lazyLoad.html">SOURCE</a> - <a href="#-api">API</a></p>
<hr>
<p><strong>Love this project? 😍 <a href="https://ko-fi.com/verlok">Buy me a coffee!</a></strong></p>
<hr>
<h2 id="📺-Demos"><a href="#📺-Demos" class="headerlink" title="📺 Demos"></a>📺 Demos</h2><p>Didn’t find the <a href="#-recipes">recipe</a> that exactly matches your case? We have demos!</p>
<p>The <a href="https://github.com/verlok/vanilla-lazyload/tree/master/demos">demos</a> folder contains 30+ use cases of vanilla-lazyload. You might find there what you’re looking for.</p>
<table>
<thead>
<tr>
<th>Type</th>
<th>Title</th>
<th>Code</th>
<th>Demo</th>
</tr>
</thead>
<tbody><tr>
<td>Content</td>
<td>Simple lazy loaded images, not using any placeholder</td>
<td><a href="demos/image_basic.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/image_basic.html">Live</a></td>
</tr>
<tr>
<td>Content</td>
<td>Lazy images that use an inline SVG as a placeholder</td>
<td><a href="demos/image_ph_inline.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/image_ph_inline.html">Live</a></td>
</tr>
<tr>
<td>Content</td>
<td>Lazy images that use an external SVG file as a placeholder</td>
<td><a href="demos/image_ph_external.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/image_ph_external.html">Live</a></td>
</tr>
<tr>
<td>Content</td>
<td>Lazy responsive images with <code>srcset</code></td>
<td><a href="demos/image_srcset.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/image_srcset.html">Live</a></td>
</tr>
<tr>
<td>Content</td>
<td>Lazy responsive images with the <code>&lt;picture&gt;</code> tag and the <code>media</code> attribute (art direction)</td>
<td><a href="demos/picture_media.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/picture_media.html">Live</a></td>
</tr>
<tr>
<td>Content</td>
<td>Lazy responsive images with <code>srcset</code> and <code>sizes</code> (using <code>data-sizes</code>)</td>
<td><a href="demos/image_srcset_lazy_sizes.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/image_srcset_lazy_sizes.html">Live</a></td>
</tr>
<tr>
<td>Content</td>
<td>Lazy responsive images with <code>srcset</code> and <code>sizes</code> (using plain <code>sizes</code>)</td>
<td><a href="demos/image_srcset_sizes.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/image_srcset_sizes.html">Live</a></td>
</tr>
<tr>
<td>Content</td>
<td>Lazy video with multiple <code>&lt;source&gt;</code> tags</td>
<td><a href="demos/video.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/video.html">Live</a></td>
</tr>
<tr>
<td>Content</td>
<td>Lazy loading background images</td>
<td><a href="demos/background_images.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/background_images.html">Live</a></td>
</tr>
<tr>
<td>Content</td>
<td>Lazy loading multiple background images</td>
<td><a href="demos/background_images_multi.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/background_images_multi.html">Live</a></td>
</tr>
<tr>
<td>Content</td>
<td>Lazy WebP images with the <code>&lt;picture&gt;</code> tag and the <code>type</code> attribute for WebP</td>
<td><a href="demos/picture_type_webp.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/picture_type_webp.html">Live</a></td>
</tr>
<tr>
<td>Loading</td>
<td>Asynchronous loading LazyLoad with requireJS</td>
<td><a href="demos/amd.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/amd.html">Live</a></td>
</tr>
<tr>
<td>Loading</td>
<td>Asynchronous loading LazyLoad + InterserctionObserver with requireJS</td>
<td><a href="demos/amd_polyfill.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/amd_polyfill.html">Live</a></td>
</tr>
<tr>
<td>Loading</td>
<td>Asynchronous loading LazyLoad with <code>&lt;script async&gt;</code></td>
<td><a href="demos/async.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/async.html">Live</a></td>
</tr>
<tr>
<td>Loading</td>
<td>Asynchronous loading multiple LazyLoad instances with <code>&lt;script async&gt;</code></td>
<td><a href="demos/async_multiple.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/async_multiple.html">Live</a></td>
</tr>
<tr>
<td>Technique</td>
<td>Fade in images as they load</td>
<td><a href="demos/fade_in.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/fade_in.html">Live</a></td>
</tr>
<tr>
<td>Technique</td>
<td>Lazily create lazyload instances</td>
<td><a href="demos/lazily_load_lazyLoad.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/lazily_load_lazyLoad.html">Live</a></td>
</tr>
<tr>
<td>Technique</td>
<td>Lazily execute functions as specific elements enter the viewport</td>
<td><a href="demos/lazy_functions.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/lazy_functions.html">Live</a></td>
</tr>
<tr>
<td>Technique</td>
<td>How to manage the print of a page with lazy images</td>
<td><a href="demos/print.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/print.html">Live</a></td>
</tr>
<tr>
<td>Technique</td>
<td>A popup layer containing lazy images in a scrolling container</td>
<td><a href="demos/popup_layer.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/popup_layer.html">Live</a></td>
</tr>
<tr>
<td>Settings</td>
<td>Multiple scrolling containers</td>
<td><a href="demos/container_multiple.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/container_multiple.html">Live</a></td>
</tr>
<tr>
<td>Settings</td>
<td>Single scrolling container</td>
<td><a href="demos/container_single.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/container_single.html">Live</a></td>
</tr>
<tr>
<td>Methods</td>
<td>How to <code>destroy()</code> LazyLoad</td>
<td><a href="demos/destroy.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/destroy.html">Live</a></td>
</tr>
<tr>
<td>Methods</td>
<td>Adding dynamic content, then <code>update()</code> LazyLoad</td>
<td><a href="demos/dynamic_content.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/dynamic_content.html">Live</a></td>
</tr>
<tr>
<td>Methods</td>
<td>Adding dynamic content, then <code>update()</code> LazyLoad passing a NodeSet of elements</td>
<td><a href="demos/dynamic_content_nodeset.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/dynamic_content_nodeset.html">Live</a></td>
</tr>
<tr>
<td>Methods</td>
<td>Load punctual images using the <code>load()</code> method</td>
<td><a href="demos/load.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/load.html">Live</a></td>
</tr>
<tr>
<td>Methods</td>
<td>Load all images at once using <code>loadAll()</code></td>
<td><a href="demos/load_all.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/load_all.html">Live</a></td>
</tr>
<tr>
<td>Test</td>
<td>Test for multiple thresholds</td>
<td><a href="demos/thresholds.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/thresholds.html">Live</a></td>
</tr>
<tr>
<td>Test</td>
<td>Test behaviour with hidden images</td>
<td><a href="demos/image_hidden.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/image_hidden.html">Live</a></td>
</tr>
<tr>
<td>Test</td>
<td>Test performance, lazy loading of hundreds of images</td>
<td><a href="demos/hundreds.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/hundreds.html">Live</a></td>
</tr>
<tr>
<td>Native</td>
<td>Test the native lazy loading of images <em>WITHOUT</em> any line of javascript, not even this script</td>
<td><a href="demos/native_lazyload.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/native_lazyload.html">Live</a></td>
</tr>
<tr>
<td>Native</td>
<td>Test the native lazy loading of images <em>conditionally</em> using the <code>use_native</code> option (see API)</td>
<td><a href="demos/native_lazyload_conditional.html">Code</a></td>
<td><a href="https://www.andreaverlicchi.eu/vanilla-lazyload/demos/native_lazyload_conditional.html">Live</a></td>
</tr>
</tbody></table>
<hr>
<p><strong>Love this project? 😍 <a href="https://ko-fi.com/verlok">Buy me a coffee!</a></strong></p>
<hr>
<h2 id="😋-Tips-amp-tricks"><a href="#😋-Tips-amp-tricks" class="headerlink" title="😋 Tips &amp; tricks"></a>😋 Tips &amp; tricks</h2><h3 id="Occupy-space-and-avoid-content-reflow"><a href="#Occupy-space-and-avoid-content-reflow" class="headerlink" title="Occupy space and avoid content reflow"></a>Occupy space and avoid content reflow</h3><p>It’s a good idea to make sure that your lazy images occupy some space even <strong>before they are loaded</strong>, otherwise the <code>img</code> elements will be shrinked to zero-height, causing your layout to reflow and making lazyload inefficient.</p>
<p>There are <a href="https://css-tricks.com/preventing-content-reflow-from-lazy-loaded-images/">many ways to avoid content reflow</a>. I’ve <a href="https://github.com/verlok/vanilla-lazyload_placeholders_test">tested three of them</a> and found that the fastest is to <strong>avoid using a placeholder at all</strong>, and use the vertical padding trick.</p>
<h4 id="Vertical-padding-trick"><a href="#Vertical-padding-trick" class="headerlink" title="Vertical padding trick"></a>Vertical padding trick</h4><pre><code class="html">&lt;div class=&quot;image-wrapper&quot;&gt;
  &lt;img
    class=&quot;lazy image&quot;
    alt=&quot;An image&quot;
    data-src=&quot;lazy.jpg&quot;
  /&gt;
&lt;/div&gt;
</code></pre>
<pre><code class="css">.image-wrapper {
  width: 100%;
  height: 0;
  padding-bottom: 150%;
  /* 👆 image height / width * 100% */
  position: relative;
}
.image {
  width: 100%;
  height: auto;
  position: absolute;
}
</code></pre>
<p>More info in <a href="http://andyshora.com/css-image-container-padding-hack.html">Sizing Fluid Image Containers with a Little CSS Padding Hack</a> by Andy Shora. Find also a useful <a href="https://css-tricks.com/snippets/sass/maintain-aspect-ratio-mixin/">SASS mixin to maintain aspect ratio</a> on CSS tricks.</p>
<h4 id="Inline-SVG"><a href="#Inline-SVG" class="headerlink" title="Inline SVG"></a>Inline SVG</h4><p>If you can’t use the vertical padding trick for some reason, the best option is to use an SVG placeholder of the same ratio of the lazy images.</p>
<pre><code class="html">&lt;img
  src=&quot;data:image/svg+xml,%3Csvg 
    xmlns=&#39;http://www.w3.org/2000/svg&#39; 
    viewBox=&#39;0 0 3 2&#39;%3E%3C/svg%3E&quot;
  data-src=&quot;//picsum.photos/900/600&quot;
  alt=&quot;Lazy loading test image&quot;
/&gt;
</code></pre>
<p>Alternatively (but less efficiently) you can use a tiny, scaled-down version of your images as a placeholder, stretching them to the final size of the images, and obtain a blur-up effect when the full images load.</p>
<p>Using a placeholder image will also make sure that browsers don’t show your <code>alt</code> content instead of the images before the lazy-loading starts.</p>
<hr>
<p><strong>Love this project? 😍 <a href="https://ko-fi.com/verlok">Buy me a coffee!</a></strong></p>
<hr>
<h2 id="🔌-API"><a href="#🔌-API" class="headerlink" title="🔌 API"></a>🔌 API</h2><h3 id="Constructor-arguments"><a href="#Constructor-arguments" class="headerlink" title="Constructor arguments"></a>Constructor arguments</h3><p>The <code>new LazyLoad()</code> instruction you execute on your page can take two parameters:</p>
<table>
<thead>
<tr>
<th>Parameter</th>
<th>What to pass</th>
<th>Required</th>
<th>Default value</th>
<th>Type</th>
</tr>
</thead>
<tbody><tr>
<td>Options</td>
<td>The option object for this instance of LazyLoad</td>
<td>No</td>
<td><code>{}</code></td>
<td>Plain Object</td>
</tr>
<tr>
<td>Nodeset</td>
<td>A NodeSet of elements to execute LazyLoad on</td>
<td>No</td>
<td><code>null</code></td>
<td>NodeSet</td>
</tr>
</tbody></table>
<p>The most common usage of LazyLoad constructor is to pass only the options object (see “options” in the next section). For example:</p>
<pre><code class="js">var aLazyLoad = new LazyLoad({
  /* options here */
});
</code></pre>
<p>In the unusual cases when you can’t select the elements using <code>elements_selector</code>, you could pass the elements set as a second parameter. It can be either a NodeSet or an array of DOM elements.</p>
<pre><code class="js">var elementsToLazyLoad = getElementSetFromSomewhere();
var aLazyLoad = new LazyLoad(
  {
    /* options here */
  },
  elementsToLazyLoad
);
</code></pre>
<h3 id="Options"><a href="#Options" class="headerlink" title="Options"></a>Options</h3><p>For every instance of <em>LazyLoad</em> you can pass in some options, to alter its default behaviour.<br>Here’s the list of the options.</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Meaning</th>
<th>Default value</th>
<th>Example value</th>
</tr>
</thead>
<tbody><tr>
<td><code>container</code></td>
<td>The scrolling container of the elements in the <code>elements_selector</code> option.</td>
<td><code>document</code></td>
<td><code>document.querySelector(&#39;.scrollPanel&#39;)</code></td>
</tr>
<tr>
<td><code>elements_selector</code></td>
<td>The CSS selector of the elements to load lazily, which will be selected as descendants of the <code>container</code> object.</td>
<td><code>&quot;.lazy&quot;</code></td>
<td><code>&quot;.lazyload&quot;</code></td>
</tr>
<tr>
<td><code>threshold</code></td>
<td>A number of pixels representing the outer distance off the scrolling area from which to start loading the elements.</td>
<td><code>300</code></td>
<td><code>0</code></td>
</tr>
<tr>
<td><code>thresholds</code></td>
<td>Similar to <code>threshold</code>, but accepting multiple values and both <code>px</code> and <code>%</code> units. It maps directly to the <code>rootMargin</code> property of IntersectionObserver (<a href="https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/rootMargin">read more</a>), so it must be a string with a syntax similar to the CSS <code>margin</code> property. You can use it when you need to have different thresholds for the scrolling area. It overrides <code>threshold</code> when passed.</td>
<td><code>null</code></td>
<td><code>&quot;500px 10%&quot;</code></td>
</tr>
<tr>
<td><code>data_src</code></td>
<td>The name of the data attribute containing the element URL to load, excluding the <code>&quot;data-&quot;</code> part. E.g. if your data attribute is named <code>&quot;data-src&quot;</code>, just pass <code>&quot;src&quot;</code></td>
<td><code>&quot;src&quot;</code></td>
<td><code>&quot;lazy-src&quot;</code></td>
</tr>
<tr>
<td><code>data_srcset</code></td>
<td>The name of the data attribute containing the image URL set to load, in either <code>img</code> and <code>source</code> tags, excluding the <code>&quot;data-&quot;</code> part. E.g. if your data attribute is named <code>&quot;data-srcset&quot;</code>, just pass <code>&quot;srcset&quot;</code></td>
<td><code>&quot;srcset&quot;</code></td>
<td><code>&quot;lazy-srcset&quot;</code></td>
</tr>
<tr>
<td><code>data_sizes</code></td>
<td>The name of the data attribute containing the sizes attribute to use, excluding the <code>&quot;data-&quot;</code> part. E.g. if your data attribute is named <code>&quot;data-sizes&quot;</code>, just pass <code>&quot;sizes&quot;</code></td>
<td><code>&quot;sizes&quot;</code></td>
<td><code>&quot;lazy-sizes&quot;</code></td>
</tr>
<tr>
<td><code>data_bg</code></td>
<td>The name of the data attribute containing the URL of <code>background-image</code> to load lazily, excluding the <code>&quot;data-&quot;</code> part. E.g. if your data attribute is named <code>&quot;data-bg&quot;</code>, just pass <code>&quot;bg&quot;</code>. The attribute value must be a valid value for <code>background-image</code>, including the <code>url()</code> part of the CSS instruction.</td>
<td><code>&quot;bg&quot;</code></td>
<td><code>&quot;lazy-bg&quot;</code></td>
</tr>
<tr>
<td><code>data_bg_hidpi</code></td>
<td>The name of the data attribute containing the URL of <code>background-image</code> to load lazily on HiDPI screens, excluding the <code>&quot;data-&quot;</code> part. E.g. if your data attribute is named <code>&quot;data-bg-hidpi&quot;</code>, just pass <code>&quot;bg-hidpi&quot;</code>. The attribute value must be a valid value for <code>background-image</code>, including the <code>url()</code> part of the CSS instruction.</td>
<td><code>&quot;bg-hidpi&quot;</code></td>
<td><code>&quot;lazy-bg-hidpi&quot;</code></td>
</tr>
<tr>
<td><code>data_bg_multi</code></td>
<td>The name of the data attribute containing the value of multiple <code>background-image</code> to load lazily, excluding the <code>&quot;data-&quot;</code> part. E.g. if your data attribute is named <code>&quot;data-bg-multi&quot;</code>, just pass <code>&quot;bg-multi&quot;</code>. The attribute value must be a valid value for <code>background-image</code>, including the <code>url()</code> part of the CSS instruction.</td>
<td><code>&quot;bg-multi&quot;</code></td>
<td><code>&quot;lazy-bg-multi&quot;</code></td>
</tr>
<tr>
<td><code>data_bg_multi_hidpi</code></td>
<td>The name of the data attribute containing the value of multiple <code>background-image</code> to load lazily on HiDPI screens, excluding the <code>&quot;data-&quot;</code> part. E.g. if your data attribute is named <code>&quot;data-bg-multi-hidpi&quot;</code>, just pass <code>&quot;bg-multi-hidpi&quot;</code>. The attribute value must be a valid value for <code>background-image</code>, including the <code>url()</code> part of the CSS instruction.</td>
<td><code>&quot;bg-multi-hidpi&quot;</code></td>
<td><code>&quot;lazy-bg-multi-hidpi&quot;</code></td>
</tr>
<tr>
<td><code>data_poster</code></td>
<td>The name of the data attribute containing the value of <code>poster</code> to load lazily, excluding the <code>&quot;data-&quot;</code> part. E.g. if your data attribute is named <code>&quot;data-poster&quot;</code>, just pass <code>&quot;poster&quot;</code>.</td>
<td><code>&quot;poster&quot;</code></td>
<td><code>&quot;lazy-poster&quot;</code></td>
</tr>
<tr>
<td><code>class_applied</code></td>
<td>The class applied to the multiple background elements after the multiple background was applied</td>
<td><code>&quot;applied&quot;</code></td>
<td><code>&quot;lazy-applied&quot;</code></td>
</tr>
<tr>
<td><code>class_loading</code></td>
<td>The class applied to the elements while the loading is in progress.</td>
<td><code>&quot;loading&quot;</code></td>
<td><code>&quot;lazy-loading&quot;</code></td>
</tr>
<tr>
<td><code>class_loaded</code></td>
<td>The class applied to the elements when the loading is complete.</td>
<td><code>&quot;loaded&quot;</code></td>
<td><code>&quot;lazy-loaded&quot;</code></td>
</tr>
<tr>
<td><code>class_error</code></td>
<td>The class applied to the elements when the element causes an error.</td>
<td><code>&quot;error&quot;</code></td>
<td><code>&quot;lazy-error&quot;</code></td>
</tr>
<tr>
<td><code>cancel_on_exit</code></td>
<td>A boolean that defines whether or not to cancel the download of the images that exit the viewport while they are still loading, eventually restoring the original attributes. It applies only to images so to the <code>img</code> (and <code>picture</code>) tags, so it doesn’t apply to background images, <code>iframe</code>s nor <code>video</code>s.</td>
<td><code>true</code></td>
<td><code>false</code></td>
</tr>
<tr>
<td><code>unobserve_entered</code></td>
<td>A boolean that defines whether or not to automatically unobserve elements once they entered the viewport</td>
<td><code>false</code></td>
<td><code>true</code></td>
</tr>
<tr>
<td><code>unobserve_completed</code></td>
<td>A boolean that defines whether or not to automatically unobserve elements once they’ve loaded or throwed an error</td>
<td><code>true</code></td>
<td><code>false</code></td>
</tr>
<tr>
<td><code>callback_enter</code></td>
<td>A callback function which is called whenever an element enters the viewport. Arguments: DOM element, intersection observer entry, lazyload instance.</td>
<td><code>null</code></td>
<td><code>(el)=&gt;{console.log(&quot;Entered&quot;, el)}</code></td>
</tr>
<tr>
<td><code>callback_exit</code></td>
<td>A callback function which is called whenever an element exits the viewport. Arguments: DOM element, intersection observer entry, lazyload instance.</td>
<td><code>null</code></td>
<td><code>(el)=&gt;{console.log(&quot;Exited&quot;, el)}</code></td>
</tr>
<tr>
<td><code>callback_loading</code></td>
<td>A callback function which is called whenever an element starts loading. Arguments: DOM element, lazyload instance.</td>
<td><code>null</code></td>
<td><code>(el)=&gt;{console.log(&quot;Loading&quot;, el)}</code></td>
</tr>
<tr>
<td><code>callback_cancel</code></td>
<td>A callback function which is called whenever an element loading is canceled while loading, as for <code>cancel_on_exit: true</code>.</td>
<td><code>null</code></td>
<td><code>(el)=&gt;{console.log(&quot;Cancelled&quot;, el)}</code></td>
</tr>
<tr>
<td><code>callback_loaded</code></td>
<td>A callback function which is called whenever an element finishes loading. Note that, in version older than 11.0.0, this option went under the name <code>callback_load</code>. Arguments: DOM element, lazyload instance.</td>
<td><code>null</code></td>
<td><code>(el)=&gt;{console.log(&quot;Loaded&quot;, el)}</code></td>
</tr>
<tr>
<td><code>callback_error</code></td>
<td>A callback function which is called whenever an element triggers an error. Arguments: DOM element, lazyload instance.</td>
<td><code>null</code></td>
<td><code>(el)=&gt;{console.log(&quot;Error&quot;, el)}</code></td>
</tr>
<tr>
<td><code>callback_applied</code></td>
<td>A callback function which is called whenever a multiple background element starts loading. Arguments: DOM element, lazyload instance.</td>
<td><code>null</code></td>
<td><code>(el)=&gt;{console.log(&quot;Applied&quot;, el)}</code></td>
</tr>
<tr>
<td><code>callback_finish</code></td>
<td>A callback function which is called when there are no more elements to load <em>and</em> all elements have been downloaded. Arguments: lazyload instance.</td>
<td><code>null</code></td>
<td><code>()=&gt;{console.log(&quot;Finish&quot;)}</code></td>
</tr>
<tr>
<td><code>use_native</code></td>
<td>This boolean sets whether or not to use <a href="https://addyosmani.com/blog/lazy-loading/">native lazy loading</a> to do <a href="https://www.smashingmagazine.com/2019/05/hybrid-lazy-loading-progressive-migration-native/">hybrid lazy loading</a>. On browsers that support it, LazyLoad will set the <code>loading=&quot;lazy&quot;</code> attribute on images and iframes, and delegate their loading to the browser.</td>
<td><code>false</code></td>
<td><code>true</code></td>
</tr>
</tbody></table>
<h3 id="Methods"><a href="#Methods" class="headerlink" title="Methods"></a>Methods</h3><p><strong>Instance methods</strong></p>
<p>You can call the following methods on any instance of LazyLoad.</p>
<table>
<thead>
<tr>
<th>Method name</th>
<th>Effect</th>
<th>Use case</th>
</tr>
</thead>
<tbody><tr>
<td><code>update()</code></td>
<td>Make LazyLoad to re-check the DOM for <code>elements_selector</code> elements inside its <code>container</code>.</td>
<td>Update LazyLoad after you added or removed DOM elements to the page.</td>
</tr>
<tr>
<td><code>loadAll()</code></td>
<td>Loads all the lazy elements right away <em>and</em> stop observing them, no matter if they are inside or outside the viewport, no matter if they are hidden or visible.</td>
<td>To load all the remaining elements in advance</td>
</tr>
<tr>
<td><code>destroy()</code></td>
<td>Destroys the instance, unsetting instance variables and removing listeners.</td>
<td>Free up some memory. Especially useful for Single Page Applications.</td>
</tr>
</tbody></table>
<p><strong>Static methods</strong></p>
<p>You can call the following static methods on the LazyLoad class itself (e.g. <code>LazyLoad.load(element, settings)</code>).</p>
<table>
<thead>
<tr>
<th>Method name</th>
<th>Effect</th>
<th>Use case</th>
</tr>
</thead>
<tbody><tr>
<td><code>load(element, settings)</code></td>
<td>Immediately loads the lazy <code>element</code>. You can pass your custom options in the <code>settings</code> parameter. Note that the <code>elements_selector</code> option has no effect, since you are passing the element as a parameter. Also note that this method has effect only once on a specific <code>element</code>.</td>
<td>To load an <code>element</code> at mouseover or at any other event different than “entering the viewport”</td>
</tr>
<tr>
<td><code>resetStatus(element)</code></td>
<td>Resets the internal status of the given <code>element</code>.</td>
<td>To tell LazyLoad to consider this <code>element</code> again, for example if you changed the <code>data-src</code> attribute after the previous <code>data-src</code> was loaded, call this method, then call <code>update()</code>.</td>
</tr>
</tbody></table>
<h3 id="Properties"><a href="#Properties" class="headerlink" title="Properties"></a>Properties</h3><p>You can use the following properties on any instance of LazyLoad.</p>
<table>
<thead>
<tr>
<th>Property name</th>
<th>Value</th>
</tr>
</thead>
<tbody><tr>
<td><code>loadingCount</code></td>
<td>The number of elements that are currently downloading from the network (limitedly to the ones managed by the instance of LazyLoad). This is particularly useful to understand whether or not is safe to destroy this instance of LazyLoad.</td>
</tr>
<tr>
<td><code>toLoadCount</code></td>
<td>The number of elements that haven’t been lazyloaded yet (limitedly to the ones managed by the instance of LazyLoad)</td>
</tr>
</tbody></table>
<hr>
<p><strong>Love this project? 😍 <a href="https://ko-fi.com/verlok">Buy me a coffee!</a></strong></p>
<hr>
<h2 id="😯-Notable-features"><a href="#😯-Notable-features" class="headerlink" title="😯 Notable features"></a>😯 Notable features</h2><h3 id="It-works-with-your-favourite-library-or-framework"><a href="#It-works-with-your-favourite-library-or-framework" class="headerlink" title="It works with your favourite library or framework"></a>It works with your favourite library or framework</h3><p>As <em>LazyLoad</em> doesn’t rely on jQuery, you can use it in web applications using <strong>Angular</strong>, <strong>React</strong> or <strong>Vue.js</strong> without the need to include jQuery.</p>
<h3 id="Intersection-Observer-API-for-optimized-CPU-usage"><a href="#Intersection-Observer-API-for-optimized-CPU-usage" class="headerlink" title="Intersection Observer API for optimized CPU usage"></a>Intersection Observer API for optimized CPU usage</h3><p>Instead of listening to the <code>scroll</code> and <code>resize</code> events, LazyLoad uses the Intersection Observer API which is a new, blazing fast method to detect if an element is inside the browser viewport. Your users will see the difference in slow and even in fast devices or computers.</p>
<h3 id="SEO-friendly"><a href="#SEO-friendly" class="headerlink" title="SEO friendly"></a>SEO friendly</h3><p><em>LazyLoad</em> <strong>doesn’t hide your images from search engines</strong>, even if you don’t specify any initial <code>src</code> for your image.</p>
<h3 id="Flaky-connections-supported"><a href="#Flaky-connections-supported" class="headerlink" title="Flaky connections supported"></a>Flaky connections supported</h3><p>If your users lose the internet connection causing errors on images loading, this script tries and loads those images again when the connection is restored.</p>
<h3 id="Optimize-for-slow-connections"><a href="#Optimize-for-slow-connections" class="headerlink" title="Optimize for slow connections"></a>Optimize for slow connections</h3><p><em>LazyLoad</em> optimizes for slow connections by cancelling the download of images when they exit the viewport.</p>
<h3 id="Support-for-responsive-images"><a href="#Support-for-responsive-images" class="headerlink" title="Support for responsive images"></a>Support for responsive images</h3><p><em>LazyLoad</em> supports responsive images, both via the <code>srcset</code> &amp; <code>sizes</code> attributes and via the <code>picture</code> tag.</p>
<h3 id="Support-for-single-and-background-images-and-HiDPI-displays"><a href="#Support-for-single-and-background-images-and-HiDPI-displays" class="headerlink" title="Support for single and background images, and HiDPI displays"></a>Support for single and background images, and HiDPI displays</h3><p><em>LazyLoad</em> supports single and multiple lazy background images, with standard resolution or HiDPI (retina) displays.</p>
<h3 id="Tested-on-real-browsers"><a href="#Tested-on-real-browsers" class="headerlink" title="Tested on real browsers"></a>Tested on real browsers</h3><p>Legacy browsers support is from IE 9 up. This script is tested in every browser before every release using <a href="http://browserstack.com/">BrowserStack</a> live, thanks to the BrowserStack Open Source initiative.</p>
<p><a href="http://browserstack.com/"><img alt="BrowserStack Logo" src="./img/browserstack-logo-600x315.png"  width="300" height="158"/></a></p>
<hr>
<p><strong>Love this project? 😍 <a href="https://ko-fi.com/verlok">Buy me a coffee!</a></strong></p>
<hr>
